package com.tedu.account.service.impl;

import com.tedu.account.mapper.AccountMapper;
import com.tedu.account.pojo.dto.AccountQuery;
import com.tedu.account.pojo.dto.AccountSaveParam;
import com.tedu.account.pojo.entity.Account;
import com.tedu.account.pojo.vo.AccountVO;
import com.tedu.account.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    AccountMapper accountMapper;

    @Override
    public void saveAccount(AccountSaveParam accountSaveParam) {
        log.debug("账户保存:{}", accountSaveParam);
        Account account = new Account();
        BeanUtils.copyProperties(accountSaveParam, account);
        if (accountSaveParam.getId()==null) {
            account.setFixedBalance(new BigDecimal("0.0"));
            account.setExpendBalance(new BigDecimal("0.0"));
            account.setSpare(new BigDecimal("0.0"));
            account.setCreateTime(new Date());
            accountMapper.saveAccount(account);
        }else {
            account.setUpdateTime(new Date());
            log.debug("账户更新:{}", account);
            accountMapper.updateAccount(account);
        }
    }

    @Override
    public List<AccountVO> selectAccount(AccountQuery accountQuery) {
        log.debug("账户查询:{}", accountQuery);
        List<AccountVO> accountList =accountMapper.selectAccount(accountQuery);
        return accountList;
    }

    @Override
    public void deleteAccount(Integer id) {
        log.debug("账户删除:{}", id);
        accountMapper.deleteAccount(id);
    }
}
